package com.vq3.digitalizationsoftwareintegrates.Utils.ImageUtil;

import lombok.extern.slf4j.Slf4j;
import net.coobird.thumbnailator.Thumbnails;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.*;

import static java.nio.charset.StandardCharsets.UTF_8;

@Slf4j
public class ImageUtil {
    /**
     * 保存图片的方法
     */
    public Map<String, String> saveImage(MultipartFile file, String path) throws IOException {

        /*生成一个全球唯一码，保证图片名不会重复*/
        Map<String, String> paramMap = new HashMap<>();
        String uuid = UUID.randomUUID().toString();
        OutputStream os = null;
        InputStream inputStream = null;
        String fileName;
        File tempFile = null;
        try {
            inputStream = file.getInputStream();
            fileName = file.getOriginalFilename();
            log.info("fileName=" + fileName);
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            /*String path = "F:\\developContext\\Img\\";*/
            // 2、保存到临时文件
            // 1K的数据缓冲
            byte[] bs = new byte[1024];
            // 读取到的数据长度
            int len;
            // 输出的文件流保存到本地文件
            tempFile = new File(path);
            if (!tempFile.exists()) {
                tempFile.mkdirs();
            }
            os = new FileOutputStream(tempFile.getPath() + File.separator + uuid+ "100" + ".jpeg");
            paramMap.put("filePath", tempFile.getAbsolutePath());
            // 开始读取
            while ((len = inputStream.read(bs)) != -1) {

                os.write(bs, 0, len);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            // 完毕，关闭所有链接
            try {
                assert os != null;
                os.close();
                assert inputStream != null;
                inputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        paramMap.put("uuid", uuid+"25");
        Thumbnails.of(tempFile.getPath() + File.separator + uuid+ "100" + ".jpeg").scale(0.25f).toFile(tempFile.getPath() + File.separator + uuid+ "25" + ".jpeg");
        deleteImage( tempFile.getPath() + File.separator +uuid+ "100" + ".jpeg");


        return paramMap;
    }
    /**读取文件的方法
     *
     * */
    public String loadImage(String path) throws IOException {
        FileInputStream fileInputStream = null;
        try {
            fileInputStream = new FileInputStream(path);
        }catch (FileNotFoundException e){
            System.out.println(path+"文件不存在");
            return null;
        }
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(fileInputStream, StandardCharsets.US_ASCII));
        StringBuilder stringBuffer = new StringBuilder();
        String line = "";
        while ((line = bufferedReader.readLine()) != null){
            if (!line.equals("")){
                stringBuffer.append(line);
            }
        }
        Test test = new Test();
        test.byteToFile(stringBuffer.toString().getBytes(StandardCharsets.US_ASCII),path);


        return stringBuffer.toString();

    }

    public boolean deleteImage(String imageName) {

        File file = new File(imageName);// 根据指定的文件名创建File对象
        boolean delete = false;
        if (!file.exists()) { // 要删除的文件不存在
            log.info("文件" + imageName + "不存在，删除失败！");
        } else { // 要删除的文件存在
            if (file.isFile()) { // 如果目标文件是文件，判断是文件
               delete = file.delete();
            }
        }
        return delete;
    }

   /* *//**
     * ASCII码转中文
     *
     * @return
     *//*
    public static byte[] ASCIItoString(String ascii) {
        StringBuilder stringBuffer = new StringBuilder();
        String[] chars = ascii.split(" ");
        for (String aChar : chars) {
            stringBuffer.append((char) Integer.parseInt(aChar));
        }
        return stringBuffer.toString().getBytes(UTF_8);
    }

    *//**
     * 中文转ASCII码
     *
     *//*
    public static String StringToASCII(String string) {
        //把字符中转换为字符数组
        char[] chars = string.toCharArray();
        StringBuilder stringBuffer = new StringBuilder();
        for (char aChar : chars) {
            stringBuffer.append((int) aChar);
            stringBuffer.append(" ");
        }
        return stringBuffer.toString();

    }*/
}